package reactor.core.publisher;

import java.util.Objects;
import java.util.function.Supplier;
import reactor.core.CoreSubscriber;

final class MonoDefer<T> extends Mono<T> implements SourceProducer<T> {

	final Supplier<? extends Mono<? extends T>> supplier;

	MonoDefer(Supplier<? extends Mono<? extends T>> supplier) {
		this.supplier = Objects.requireNonNull(supplier, "supplier");
	}

	public void subscribe(CoreSubscriber<? super T> actual) {
		Mono<? extends T> mono;
		try {
			mono = Objects.requireNonNull(supplier.get(), "The Mono returned by the supplier is null");
		} catch (Throwable e) {
			Operators.error(actual, Operators.onOperatorError(e, actual.currentContext()));
			return;
		}
		mono.subscribe(actual);
	}

	@Override
	public Object scanUnsafe(Attr key) {
		return null;
	}
}
